package com.ddone.mapper;

import com.ddone.bean.Stock;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author ddone
 * @date 2024/4/6-14:17
 * JpaRepository<Stock,Integer> 对象类型和ID类型
 */
public interface StockMapper extends JpaRepository<Stock,Integer> {

    /**
     *   Spring Data JPA会根据我们所定义的⽅法名，⾃动⽣成对应的sql语句(方法结构需要满足要求)
     *   文档地址: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/
     *   @Query：自定义sql实现查询，@Modifying：指定sql为更新,不添加此注解表示查询
     *   更新需要添加事务注解
     * @param productId
     * @param count
     * @return
     */
    @Transactional
    @Modifying
    @Query("update Stock s set s.count = s.count - ?2 where s.productId = ?1")
    int updateStockByProductId(String productId, Integer count);
}

